package com.xzg.mission.controller;

import com.xzg.common.constants.Constant;
import com.xzg.common.exception.XZGException;
import com.xzg.common.page.PageQueryBean;
import com.xzg.common.resp.APIResult;
import com.xzg.common.utils.DateUtil;
import com.xzg.common.vo.QueryCondition;
import com.xzg.middleTable.dao.CategoryMapper;
import com.xzg.middleTable.entity.Category;
import com.xzg.middleTable.entity.Tmission;
import com.xzg.mission.Service.MissionService;
import com.xzg.mission.entity.Mission;
import com.xzg.user.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping("/mission")
public class MissionController {

    @Autowired
    private MissionService missionService;


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值，false:不能为空值
    }

    /**
     * @Author Ye
     * Date 2018-03-05 16:19
     * @Description 跳转任务页面
     */
    @RequestMapping
    public String mission() {
        return "mission";
    }

    /**
     * @Author Ye
     * Date 2018-03-03 11:19
     * @Description 查询所有任务
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageQueryBean missionList(@RequestBody QueryCondition condition) {

        //切割日期
        if (condition.getRangeDate() != null) {
            String[] range = condition.getRangeDate().split("/");
            condition.setStartDate(range[0]);
            condition.setEndDate(range[1]);
        }
        PageQueryBean result = missionService.listMission(condition);
        return result;
    }

    /**
     * @Author Ye
     * Date 2018-03-03 11:19
     * @Description 查询所有任务
     */
    @RequestMapping("/wlist")
    @ResponseBody
    public PageQueryBean wmissionList(QueryCondition condition) {

        //切割日期
        if (condition.getRangeDate() != null) {
            String[] range = condition.getRangeDate().split("/");
            condition.setStartDate(range[0]);
            condition.setEndDate(range[1]);
        }
        PageQueryBean result = missionService.listMission(condition);
        return result;
    }

    /**
     * @Author Ye
     * Date 2018-03-06 16:05
     * @Description 查询任务不查询内容
     */
    @RequestMapping("/part")
    @ResponseBody
    public PageQueryBean missionPart(@RequestBody QueryCondition condition) throws ParseException {
        //切割日期
        String[] range = condition.getRangeDate().split("/");
        condition.setStartDate(range[0]);
        condition.setEndDate(range[1]);
        PageQueryBean result = missionService.missionPart(condition);
        return result;
    }

    /**
     * @Author Ye
     * Date 2018-03-07 16:48
     * @Description 根据Id查询单个任务
     */
    @RequestMapping("/findOne")
    @ResponseBody
    public APIResult findOne(@RequestBody QueryCondition condition) throws XZGException {
        APIResult<Mission> resp = new APIResult();
        Mission mission = missionService.findOne(condition.getMissionId());
        if (mission != null) {
            resp.setCode(Constant.RESP_STATUS_OK);
            resp.setMessage("查询成功");
            resp.setData(mission);
        }
        return resp;
    }

    /**
     * @Author Ye
     * Date 2018-03-06 16:04
     * @Description 添加任务
     */
    @RequestMapping("/publish")
    @ResponseBody
    public APIResult publish(HttpSession session, String missionName, int award, int deduct, String endDate, int status, String content, int manNum) {
        APIResult<String> resp = new APIResult<>();
        User user = (User) session.getAttribute("userinfo");
        Mission mission = new Mission();
        mission.setMissionName(missionName);
        mission.setAward(award);
        mission.setDeduct(deduct);
        mission.setStatus((byte) status);
        mission.setManNum(manNum);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = formatter.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        mission.setEndDate(parse);
        mission.setStartDate(new Date());
        mission.setMissionDate(new Date());
        mission.setContent(content);
        mission.setPublisher(user.getUsername());
        mission.setPublisherId(user.getId());
//        mission.setDepartment(user.getDepartment());
//        mission.setJob(user.getPosition());
        int count = missionService.createMission(mission);
        if (count > 0) {
            resp.setMessage("发布成功");
        } else {
            resp.setMessage("发布失败");
            resp.setCode(Constant.RESP_STATUS_INTERNAL_ERROR);
        }
        return resp;
    }

    @RequestMapping("/appPublish")
    @ResponseBody
    public APIResult appPublisher(@RequestBody Mission mission) {
        APIResult<String> resp = new APIResult<>();
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
        mission.setPublisher(user.getUsername());
        mission.setPublisherId(user.getId());
        mission.setStartDate(new Date());
        if (new Date() != mission.getEndDate()) {
            Calendar cl = Calendar.getInstance();
            cl.setTime(mission.getEndDate());
            java.util.Date date = DateUtil.getBeforeDay(cl).getTime();
            System.out.println(cl.getTime());
            mission.setMissionDate(date);
        } else {
            mission.setMissionDate(mission.getEndDate());
        }
        int count = missionService.createMission(mission);
        if (count > 0) {
            resp.setMessage("发布成功");
        } else {
            resp.setMessage("发布失败");
            resp.setCode(Constant.RESP_STATUS_INTERNAL_ERROR);
        }
        return resp;
    }

    /**
     * @Author Ye
     * Date 2018-03-07 16:17
     * @Description 删除任务
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteMission(Long id) {
        missionService.deleteMission(id);
        return "del_succ";
    }

    /**
     * @Author: ye
     * @Date: 10:24 2018\4\12 0012
     * @Description 添加標籤
     */
    @RequestMapping("/insertCategory")
    @ResponseBody
    public APIResult insertCategory(@RequestBody Category category) {
        APIResult resp = missionService.inserCategory(category);
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 10:24 2018\4\12 0012
     * @Description 查询所有标签
     */
    @RequestMapping("categoryAll")
    @ResponseBody
    public APIResult categoryAll() {
        APIResult resp = missionService.categoryAll();
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 13:36 2018\4\12 0012
     * @Description app发布积分模板
     */
    @RequestMapping("/categoryMission")
    @ResponseBody
    public APIResult categoryMission(@RequestBody Tmission mission) {
        APIResult resp = missionService.categoryMission(mission);
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 13:36 2018\4\12 0012
     * @Description 网站发布积分模板
     */
    @RequestMapping("/wcategoryMission")
    @ResponseBody
    public APIResult wcategoryMission(Tmission mission) {
        APIResult resp = missionService.categoryMission(mission);
        return resp;
    }

    @RequestMapping("/deleteCategory")
    @ResponseBody
    public APIResult deleteCategory(@RequestBody Category category) {
        APIResult resp = missionService.deleteCategory(category.getCategoryCid());
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 13:43 2018\4\12 0012
     * @Description 网页查看所有积分模板
     */
    @RequestMapping("/findTMission")
    @ResponseBody
    public PageQueryBean findTMission(QueryCondition condition) {
        PageQueryBean resp = missionService.findTMission(condition);
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 13:43 2018\4\12 0012
     * @Description app查看所有积分模板
     */
    @RequestMapping("/APPfindTMission")
    @ResponseBody
    public PageQueryBean APPfindTMission(@RequestBody QueryCondition condition) {
        PageQueryBean resp = missionService.findTMission(condition);
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 14:00 2018\4\12 0012
     * @Description 修改积分模板
     */
    @RequestMapping("/updateTMission")
    @ResponseBody
    public APIResult updateTMission(@RequestBody Tmission tmission) {
        APIResult resp = missionService.updateTMission(tmission);
        return resp;
    }

    /**
     * @Author: ye
     * @Date: 14:04 2018\4\12 0012
     * @Description 删除积分模板
     */
    @RequestMapping("/deleteTMission")
    @ResponseBody
    public APIResult deleteTMission(Tmission tmission) {
        APIResult resp = missionService.deleteTMission(tmission);
        return resp;
    }
}
